package com.phone.sdktest;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Debug;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.phone.sdktest.window.MiWindow;
import com.phone.sdktest.window.NormalWindow;

public class MainActivity extends AppCompatActivity {


    private String TAG = "jsTest";
    private Context context;

    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SYSTEM_ALERT_WINDOW};
    private WmDialog mWmDialog;
    private WebView webview;
    private Button button;
    private WmDialog wmDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this.getApplicationContext();


//        webview = new WebView(context);
//        webview.loadUrl("https://www.baidu.com");

        button = new Button(context);
        button.setText("=====");
        button.setBackgroundColor(Color.BLACK);
        button.setTextColor(Color.BLUE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "=========", Toast.LENGTH_SHORT).show();
            }
        });


        findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

//                Subscription subscription = new Subscription();
//                subscription.clickUrl = "https://www.baidu.com";
//                new JSFeatureManager(context).startCollect(subscription);

                new WmDialog(true, button).show();


            }
        });

        findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new WmDialog(false, button).show();
            }
        });


        /*
        WindowManager 悬浮窗口实现

        在23以下，在清单文件中添加SYSTEM_ALERT_WINDOW权限，自动获取权限
            可以使用WindowManager.LayoutParams.TYPE_PHONE、WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

        在23以上、25以下，SYSTEM_ALERT_WINDOW权限这不能通过代码申请方式获取，必须得用户打开软件设置页手动打开，才能授权【类似辅助功能、通知栏权限的开启方式】
            可以使用WindowManager.LayoutParams.TYPE_TOAST，不需要权限

        在25以上，WindowManager.LayoutParams.TYPE_TOAST有问题，暂时不兼容


        WindowManager.LayoutParams.TYPE_TOAST的问题：
            从19开始, 使用TYPE_TOAST的同时还可以接收触摸事件和按键事件了；
            而在19以前只能显示出来, 不能交互

        小米手机兼容
        http://www.mamicode.com/info-detail-1426962.html
        */

    }


    private boolean canUseSystemDialog() {
        return Build.VERSION.SDK_INT <= 22 && hasPermission(context, Manifest.permission.SYSTEM_ALERT_WINDOW);
    }

    private boolean hasPermission(Context context, String permission) {
        int perm = context.checkCallingOrSelfPermission(permission);
        return perm == PackageManager.PERMISSION_GRANTED;
    }

    private class WmDialog {
        private WindowManager windowManager;
        private WindowManager.LayoutParams params;
        private boolean mShowing = false;
        private View mView;

        WmDialog(boolean isSystemDialog, View view) {
            mView = view;

            params = new WindowManager.LayoutParams();
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.format = PixelFormat.TRANSLUCENT;
            // 悬浮窗类型，整个demo的关键点
            params.type = isSystemDialog ? WindowManager.LayoutParams.TYPE_TOAST : WindowManager.LayoutParams.TYPE_PHONE;
            params.gravity = Gravity.LEFT | Gravity.TOP;
            params.setTitle("Toast");
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);


        }

        void show() {
            if (mView.getParent() != null) {
                windowManager.removeView(mView);
            }

            if (!mShowing && mView != null) {
                try {
                    windowManager.addView(mView, params);
                    mShowing = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    mShowing = false;
                }
            }
        }

        void dismiss() {
            try {
                if (mView != null && mShowing) {
                    mShowing = false;
                    windowManager.removeView(mView);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }




}
